package com.datastructure.test.maxlengthequalkinarray;

public class MaxLengthEqualKInArray {

    public static void main(String[] args) {
        int[] data = {0,1,2,3};
        MaxLengthEqualKInArray maxLengthEqualKInArray = new MaxLengthEqualKInArray();
        int i = maxLengthEqualKInArray.maxlenEqualK(data, 3);
        System.out.println(i);
    }

    public int maxlenEqualK (int[] arr, int k) {
        // write code here
        int length = arr.length;
        int max = 0;
        for (int i = length; i > 0; i--) {
            for(int j = 0;j<length-i+1;j++){
                if(compute(j,i,arr,k)){
                    return i;
                }
            }
        }
        return max;
    }

    private boolean compute(int j, int i, int[] arr, int k) {
        int sum = 0;
        for(int g =j;g<j+i;g++){
            sum+=arr[g];
        }
        return sum == k;
    }
}
